home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / pcl / src-16f.lha / compiler / dump.lisp.CKP < prev    next >
Encoding:
Text File  |  1992-10-10  |  42.2 KB  |  1,280 lines

  1. ;;; -*- Package: C; Log: C.Log -*-
  2. ;;;
  3. ;;; **********************************************************************
  4. ;;; This code was written as part of the CMU Common Lisp project at
  5. ;;; Carnegie Mellon University, and has been placed in the public domain.
  6. ;;; If you want to use this code or any part of CMU Common Lisp, please contact
  7. ;;; Scott Fahlman or slisp-group@cs.cmu.edu.
  8. ;;;
  9. (ext:file-comment
  10.   "$Header: dump.lisp,v 1.36 91/12/21 23:07:24 wlott Locked $")
  11. ;;;
  12. ;;; **********************************************************************
  13. ;;;
  14. ;;; $Header: dump.lisp,v 1.36 91/12/21 23:07:24 wlott Locked $
  15. ;;;
  16. ;;;    This file contains stuff that knows about dumping FASL files.
  17. ;;;
  18. (in-package "C")
  19.  
  20. (proclaim '(special compiler-version))
  21.  
  22. ;;;; Fasl dumper state:
  23.  
  24. ;;; We do some buffering in front of the stream that represents the output file
  25. ;;; so as to speed things up a bit.
  26. ;;;
  27. (defconstant fasl-buffer-size 2048)
  28.  
  29. ;;; The Fasl-File structure represents everything we need to know about dumping
  30. ;;; to a fasl file.  We need to objectify the state, since the fasdumper must
  31. ;;; be reentrant.
  32. ;;;
  33. (defstruct (fasl-file
  34.         (:print-function
  35.          (lambda (s stream d)
  36.            (declare (ignore d) (stream stream))
  37.            (format stream "#<Fasl-File ~S>"
  38.                (namestring (fasl-file-stream s))))))
  39.   ;;
  40.   ;; The stream we dump to.
  41.   (stream (required-argument) :type stream)
  42.   ;;
  43.   ;; The buffer we accumulate output in before blasting it out to the stream
  44.   ;; with SYS:OUTPUT-RAW-BYTES.
  45.   (buffer (make-array fasl-buffer-size :element-type '(unsigned-byte 8))
  46.       :type (simple-array (unsigned-byte 8) (*)))
  47.   ;;
  48.   ;; The index of the first free byte in Buffer.  Note that there is always at
  49.   ;; least one byte free.
  50.   (buffer-index 0 :type index)
  51.   ;;
  52.   ;; Hashtables we use to keep track of dumped constants so that we can get
  53.   ;; them from the table rather than dumping them again.  The EQUAL-TABLE is
  54.   ;; used for lists and strings, and the EQ-TABLE is used for everything else.
  55.   ;; We use a separate EQ table to avoid performance patholigies with objects
  56.   ;; for which EQUAL degnerates to EQL.  Everything entered in the EQUAL table
  57.   ;; is also entered in the EQ table.
  58.   (equal-table (make-hash-table :test #'equal) :type hash-table)
  59.   (eq-table (make-hash-table :test #'eq) :type hash-table)
  60.   ;;
  61.   ;; The table's current free pointer: the next offset to be used.
  62.   (table-free 0 :type index)
  63.   ;;
  64.   ;; Alist (Package . Offset) of the table offsets for each package we have
  65.   ;; currently located.
  66.   (packages () :type list)
  67.   ;;
  68.   ;; Table mapping from the Entry-Info structures for dumped XEPs to the table
  69.   ;; offsets of the corresponding code pointers.
  70.   (entry-table (make-hash-table :test #'eq) :type hash-table)
  71.   ;;
  72.   ;; Table holding back-patching info for forward references to XEPs.  The key
  73.   ;; is the Entry-Info structure for the XEP, and the value is a list of conses
  74.   ;; (<code-handle> . <offset>), where <code-handle> is the offset in the table
  75.   ;; of the code object needing to be patched, and <offset> is the offset that
  76.   ;; must be patched.
  77.   (patch-table (make-hash-table :test #'eq) :type hash-table)
  78.   ;;
  79.   ;; A list of the table handles for all of the DEBUG-INFO structures dumped in
  80.   ;; this file.  These structures must be back-patched with source location
  81.   ;; information when the compilation is complete.
  82.   (debug-info () :type list)
  83.   ;;
  84.   ;; Used to keep track of objects that we are in the process of dumping so
  85.   ;; that circularities can be preserved.  The key is the object that we have
  86.   ;; previously seen, and the value is the object that we reference in the
  87.   ;; table to find this previously seen object.  (The value is never NIL.)
  88.   ;;
  89.   ;; Except with list objects, the key and the value are always the same.  In a
  90.   ;; list, the key will be some tail of the value.
  91.   (circularity-table (make-hash-table :test #'eq) :type hash-table)
  92.   ;;
  93.   ;; Hash table of structures that are allowed to be dumped.  If we try to
  94.   ;; dump a structure that isn't in this hash table, we lose.
  95.   (valid-structures (make-hash-table :test #'eq) :type hash-table))
  96.  
  97. ;;; This structure holds information about a circularity.
  98. ;;;
  99. (defstruct circularity
  100.   ;;
  101.   ;; Kind of modification to make to create circularity.
  102.   (type (required-argument) :type (member :rplaca :rplacd :svset :struct-set))
  103.   ;;
  104.   ;; Object containing circularity.
  105.   object
  106.   ;;
  107.   ;; Index in object for circularity.
  108.   (index (required-argument) :type index)
  109.   ;;
  110.   ;; The object to be stored at Index in Object.  This is that the key that we
  111.   ;; were using when we discovered the circularity.
  112.   value
  113.   ;;
  114.   ;; The value that was associated with Value in the CIRCULARITY-TABLE.  This
  115.   ;; is the object that we look up in the EQ-TABLE to locate Value.
  116.   enclosing-object)
  117.  
  118.  
  119. ;;; A list of the Circularity structures for all of the circularities detected
  120. ;;; in the the current top-level call to Dump-Object.  Setting this lobotomizes
  121. ;;; circularity detection as well, since circular dumping uses the table.
  122. ;;;
  123. (defvar *circularities-detected*)
  124.  
  125.  
  126. ;;; Used to inhibit table access when dumping forms to be read by the cold
  127. ;;; loader.
  128. ;;;
  129. (defvar *cold-load-dump* nil)
  130.  
  131.  
  132. ;;; Used to turn off the structure validation during dumping of source info.
  133. ;;;
  134. (defvar *dump-only-valid-structures* t)
  135.  
  136.  
  137. ;;;; Utilities:
  138.  
  139. ;;; FLUSH-FASL-FILE-BUFFER  --  Internal
  140. ;;;
  141. ;;;    Write out the contents of File's buffer to its stream.
  142. ;;;
  143. (defun flush-fasl-file-buffer (file)
  144.   (system:output-raw-bytes (fasl-file-stream file)
  145.                (fasl-file-buffer file)
  146.                0
  147.                (fasl-file-buffer-index file))
  148.   (setf (fasl-file-buffer-index file) 0)
  149.   (undefined-value))
  150.  
  151.  
  152. ;;; Dump-Byte  --  Internal
  153. ;;;
  154. ;;;    Write the byte B to the specified fasl-file stream.
  155. ;;;
  156. (declaim (maybe-inline dump-byte))
  157. (defun dump-byte (b file)
  158.   (declare (type (unsigned-byte 8) b) (type fasl-file file)
  159.        (optimize (speed 3) (safety 0)))
  160.   (let ((idx (fasl-file-buffer-index file))
  161.     (buf (fasl-file-buffer file)))
  162.     (setf (aref buf idx) b)
  163.     (let ((new (1+ idx)))
  164.       (setf (fasl-file-buffer-index file) new)
  165.       (when (= new fasl-buffer-size)
  166.     (flush-fasl-file-buffer file))))
  167.   (undefined-value))
  168.  
  169.  
  170. ;;; DUMP-UNSIGNED-32  --  Internal
  171. ;;;
  172. ;;;    Dump a 4 byte unsigned integer.
  173. ;;;
  174. (defun dump-unsigned-32 (num file)
  175.   (declare (type (unsigned-byte 32) num) (type fasl-file file)
  176.        (optimize (speed 3) (safety 0)))
  177.   (let* ((idx (fasl-file-buffer-index file))
  178.      (buf (fasl-file-buffer file))
  179.      (new (+ idx 4)))
  180.     (when (>= new fasl-buffer-size)
  181.       (flush-fasl-file-buffer file)
  182.       (setq idx 0  new 4))
  183.     (setf (aref buf (+ idx 0)) (ldb (byte 8 0) num))
  184.     (setf (aref buf (+ idx 1)) (ldb (byte 8 8) num))
  185.     (setf (aref buf (+ idx 2)) (ldb (byte 8 16) num))
  186.     (setf (aref buf (+ idx 3)) (ldb (byte 8 24) num))
  187.     (setf (fasl-file-buffer-index file) new))
  188.   (undefined-value))
  189.  
  190.  
  191. ;;; Dump-Var-Signed   --  Internal
  192. ;;;
  193. ;;;    Dump Num to the fasl stream, represented by the specified number of
  194. ;;; bytes.
  195. ;;;
  196. (defun dump-var-signed  (num bytes file)
  197.   (declare (integer num) (type index bytes) (type fasl-file file)
  198.        (inline dump-byte))
  199.   (do ((n num (ash n -8))
  200.        (i bytes (1- i)))
  201.       ((= i 0))
  202.     (declare (type index i))
  203.     (dump-byte (logand n #xFF) file))
  204.   (undefined-value))
  205.  
  206.  
  207. ;;; DUMP-BYTES  --  Internal
  208. ;;;
  209. ;;;    Dump the first N bytes in Vec out to File.  Vec is some sort of unboxed
  210. ;;; vector-like thing that we can BLT from.
  211. ;;;
  212. (defun dump-bytes (vec n file)
  213.   (declare (type index n) (type fasl-file file)
  214.        (optimize (speed 3) (safety 0)))
  215.   (let* ((idx (fasl-file-buffer-index file))
  216.      (buf (fasl-file-buffer file))
  217.      (new (+ idx n)))
  218.     (cond ((< new fasl-buffer-size)
  219.        (bit-bash-copy vec vector-data-bit-offset
  220.               buf
  221.               (+ vector-data-bit-offset
  222.                  (the index (* idx vm:byte-bits)))
  223.               (* n vm:byte-bits))
  224.        (setf (fasl-file-buffer-index file) new))
  225.       (t
  226.        (flush-fasl-file-buffer file)
  227.        (cond ((>= n fasl-buffer-size)
  228.           (system:output-raw-bytes (fasl-file-stream file)
  229.                        vec 0 n))
  230.          (t
  231.           (bit-bash-copy vec vector-data-bit-offset
  232.                  buf vector-data-bit-offset
  233.                  (* n vm:byte-bits))
  234.           (setf (fasl-file-buffer-index file) n))))))
  235.   (undefined-value))
  236.  
  237.  
  238. ;;; Dump-FOP  --  Internal
  239. ;;;
  240. ;;;    Dump the FOP code for the named FOP to the specified fasl-file.
  241. ;;;
  242. (defmacro dump-fop (fs file)
  243.   (let* ((fs (eval fs))
  244.      (val (get fs 'lisp::fop-code)))
  245.     (assert val () "Compiler bug: ~S not a legal fasload operator." fs)
  246.     `(dump-byte ',val ,file)))
  247.  
  248.  
  249. ;;; Dump-FOP*  --  Internal
  250. ;;;
  251. ;;;    Dump a FOP-Code along with an integer argument, choosing the FOP based
  252. ;;; on whether the argument will fit in a single byte.
  253. ;;;
  254. (defmacro dump-fop* (n byte-fop word-fop file)
  255.   (once-only ((n-n n)
  256.           (n-file file))
  257.     `(cond ((< ,n-n 256)
  258.         (dump-fop ',byte-fop ,n-file)
  259.         (dump-byte ,n-n ,n-file))
  260.        (t
  261.         (dump-fop ',word-fop ,n-file)
  262.         (dump-unsigned-32 ,n-n ,n-file)))))
  263.  
  264.  
  265. ;;; Dump-Push  --  Internal
  266. ;;;
  267. ;;;    Push the object at table offset Handle on the fasl stack.
  268. ;;;
  269. (defun dump-push (handle file)
  270.   (declare (type index handle) (type fasl-file file))
  271.   (dump-fop* handle lisp::fop-byte-push lisp::fop-push file)
  272.   (undefined-value))
  273.  
  274.  
  275. ;;; Dump-Pop  --  Internal
  276. ;;;
  277. ;;;    Pop the object currently on the fasl stack top into the table, and
  278. ;;; return the table index, incrementing the free pointer.
  279. ;;;
  280. (defun dump-pop (file)
  281.   (prog1 (fasl-file-table-free file)
  282.     (dump-fop 'lisp::fop-pop file)
  283.     (incf (fasl-file-table-free file))))
  284.  
  285.  
  286. ;;; EQUAL-CHECK-TABLE  --  Internal
  287. ;;;
  288. ;;;    If X is in File's EQUAL-TABLE, then push the object and return T,
  289. ;;; otherwise NIL.  If *COLD-LOAD-DUMP* is true, then do nothing and return
  290. ;;; NIL.
  291. ;;;
  292. (defun equal-check-table (x file)
  293.   (declare (type fasl-file file))
  294.   (unless *cold-load-dump*
  295.     (let ((handle (gethash x (fasl-file-equal-table file))))
  296.       (cond (handle
  297.          (dump-push handle file)
  298.          t)
  299.         (t
  300.          nil)))))
  301.  
  302.  
  303. ;;; EQ-SAVE-OBJECT, EQUAL-SAVE-OBJECT  --  Internal
  304. ;;;
  305. ;;;    These functions are called after dumping an object to save the object in
  306. ;;; the table.  The object (also passed in as X) must already be on the top of
  307. ;;; the FOP stack.  If *COLD-LOAD-DUMP* is true, then we don't do anything.
  308. ;;;
  309. (defun eq-save-object (x file)
  310.   (declare (type fasl-file file))
  311.   (unless *cold-load-dump*
  312.     (let ((handle (dump-pop file)))
  313.       (setf (gethash x (fasl-file-eq-table file)) handle)
  314.       (dump-push handle file)))
  315.   (undefined-value))
  316. ;;;
  317. (defun equal-save-object (x file)
  318.   (declare (type fasl-file file))
  319.   (unless *cold-load-dump*
  320.     (let ((handle (dump-pop file)))
  321.       (setf (gethash x (fasl-file-equal-table file)) handle)
  322.       (setf (gethash x (fasl-file-eq-table file)) handle)
  323.       (dump-push handle file)))
  324.   (undefined-value))
  325.  
  326.  
  327. ;;; NOTE-POTENTIAL-CIRCULARITY  --  Internal
  328. ;;;
  329. ;;;    Record X in File's CIRCULARITY-TABLE unless *COLD-LOAD-DUMP* is true.
  330. ;;; This is called on objects that we are about to dump might have a circular
  331. ;;; path through them.
  332. ;;;
  333. ;;; The object must not currently be in this table, since the dumper should
  334. ;;; never be recursively called on a circular reference.  Instead, the dumping
  335. ;;; function must detect the circularity and arrange for the dumped object to
  336. ;;; be patched.
  337. ;;;
  338. (defun note-potential-circularity (x file)
  339.   (unless *cold-load-dump*
  340.     (let ((circ (fasl-file-circularity-table file)))
  341.       (assert (not (gethash x circ)))
  342.       (setf (gethash x circ) x)))
  343.   (undefined-value))
  344.  
  345.  
  346. ;;; Fasl-Dump-Cold-Load-Form  --  Interface
  347. ;;;
  348. ;;;    Dump Form to a fasl file so that it evaluated at load time in normal
  349. ;;; load and at cold-load time in cold load.  This is used to dump package
  350. ;;; frobbing forms.
  351. ;;;
  352. (defun fasl-dump-cold-load-form (form file)
  353.   (declare (type fasl-file file))
  354.   (dump-fop 'lisp::fop-normal-load file)
  355.   (let ((*cold-load-dump* t))
  356.     (dump-object form file))
  357.   (dump-fop 'lisp::fop-eval-for-effect file)
  358.   (dump-fop 'lisp::fop-maybe-cold-load file)
  359.   (undefined-value))
  360.  
  361.  
  362. ;;;; Opening and closing:
  363.  
  364. ;;; Open-Fasl-File  --  Interface
  365. ;;;
  366. ;;;    Return a Fasl-File object for dumping to the named file.  Some
  367. ;;; information about the source is specified by the string Where.
  368. ;;;
  369. (defun open-fasl-file (name where)
  370.   (declare (type pathname name))
  371.   (let* ((stream (open name :direction :output
  372.                :if-exists :new-version
  373.                :element-type '(unsigned-byte 8)))
  374.      (res (make-fasl-file :stream stream)))
  375.     (format stream
  376.         "FASL FILE output from ~A.~@
  377.         Compiled ~A on ~A~@
  378.         Compiler ~A, Lisp ~A~@
  379.         Targeted for ~A, FASL version ~D~%"
  380.         where
  381.         (ext:format-universal-time nil (get-universal-time))
  382.         (machine-instance) compiler-version
  383.         (lisp-implementation-version)
  384.         (backend-version *backend*)
  385.         (backend-fasl-file-version *backend*))
  386.     ;;
  387.     ;; Terminate header.
  388.     (dump-byte 255 res)
  389.     ;;
  390.     ;; Specify code format.
  391.     (dump-fop 'lisp::fop-code-format res)
  392.     (dump-byte (backend-fasl-file-implementation *backend*) res)
  393.     (dump-byte (backend-fasl-file-version *backend*) res)
  394.  
  395.     res))
  396.  
  397.  
  398. ;;; Close-Fasl-File  --  Interface
  399. ;;;
  400. ;;;    Close the specified Fasl-File, aborting the write if Abort-P is true.
  401. ;;; We do various sanity checks, then end the group.
  402. ;;;
  403. (defun close-fasl-file (file abort-p)
  404.   (declare (type fasl-file file))
  405.   (assert (zerop (hash-table-count (fasl-file-patch-table file))))
  406.   (dump-fop 'lisp::fop-verify-empty-stack file)
  407.   (dump-fop 'lisp::fop-verify-table-size file)
  408.   (dump-unsigned-32 (fasl-file-table-free file) file)
  409.   (dump-fop 'lisp::fop-end-group file)
  410.   (flush-fasl-file-buffer file)
  411.   (close (fasl-file-stream file) :abort abort-p)
  412.   (undefined-value))
  413.  
  414.  
  415. ;;;; Component (function) dumping:
  416.  
  417. ;;; Dump-Code-Object  --  Internal
  418. ;;;
  419. ;;;    Dump out the constant pool and code-vector for component, push the
  420. ;;; result in the table and return the offset.
  421. ;;;
  422. ;;;    The only tricky thing is handling constant-pool references to functions.
  423. ;;; If we have already dumped the function, then we just push the code pointer.
  424. ;;; Otherwise, we must create back-patching information so that the constant
  425. ;;; will be set when the function is eventually dumped.  This is a bit awkward,
  426. ;;; since we don't have the handle for the code object being dumped while we
  427. ;;; are dumping its constants.
  428. ;;;
  429. ;;;    We dump a trap object as a placeholder for the code vector, which is
  430. ;;; actually filled in by the loader.
  431. ;;;
  432. (defun dump-code-object (component code-segment code-length trace-table file)
  433.   (declare (type component component) (type fasl-file file)
  434.        (list trace-table) (type index code-length))
  435.   (let* ((2comp (component-info component))
  436.      (constants (ir2-component-constants 2comp))
  437.      (num-consts (length constants))
  438.      (trace-table (pack-trace-table trace-table))
  439.      (trace-table-length (length trace-table))
  440.      (total-length (+ code-length (* trace-table-length 2))))
  441.     (collect ((patches))
  442.       ;; Dump the offset of the trace table.
  443.       (dump-object code-length file)
  444.  
  445.       ;; Dump the constants, noting any :entries that have to be fixed up.
  446.       (do ((i vm:code-constants-offset (1+ i)))
  447.       ((>= i num-consts))
  448.     (let ((entry (aref constants i)))
  449.       (etypecase entry
  450.         (constant
  451.          (dump-object (constant-value entry) file))
  452.         (cons
  453.          (ecase (car entry)
  454.            (:entry
  455.         (let* ((info (leaf-info (cdr entry)))
  456.                (handle (gethash info (fasl-file-entry-table file))))
  457.           (cond
  458.            (handle
  459.             (dump-push handle file))
  460.            (t
  461.             (patches (cons info i))
  462.             (dump-fop 'lisp::fop-misc-trap file)))))
  463.            (:load-time-value
  464.         (dump-push (cdr entry) file))))
  465.         (null
  466.          (dump-fop 'lisp::fop-misc-trap file)))))
  467.  
  468.       ;; Dump the debug info.
  469.       (let ((info (debug-info-for-component component))
  470.         (*dump-only-valid-structures* nil))
  471.     (dump-object info file)
  472.     (let ((info-handle (dump-pop file)))
  473.       (dump-push info-handle file)
  474.       (push info-handle (fasl-file-debug-info file))))
  475.  
  476.       (let ((num-consts (- num-consts vm:code-trace-table-offset-slot)))
  477.     (cond ((and (< num-consts #x100) (< total-length #x10000))
  478.            (dump-fop 'lisp::fop-small-code file)
  479.            (dump-byte num-consts file)
  480.            (dump-var-signed total-length 2 file))
  481.           (t
  482.            (dump-fop 'lisp::fop-code file)
  483.            (dump-unsigned-32 num-consts file)
  484.            (dump-unsigned-32 total-length file))))
  485.  
  486.       (flush-fasl-file-buffer file)
  487.       (let ((fixups (emit-code-vector (fasl-file-stream file) code-segment)))
  488.     (dump-i-vector trace-table file t)
  489.     (let ((handle (dump-pop file)))
  490.       (dump-fixups handle fixups file)
  491.       (dolist (patch (patches))
  492.         (push (cons handle (cdr patch))
  493.           (gethash (car patch) (fasl-file-patch-table file))))
  494.       handle)))))
  495.  
  496.  
  497. (defun dump-assembler-routines (code-segment length routines file)
  498.   (dump-fop 'lisp::fop-assembler-code file)
  499.   (dump-unsigned-32 length file)
  500.   (flush-fasl-file-buffer file)
  501.   (let ((fixups (emit-code-vector (fasl-file-stream file) code-segment)))
  502.     (dolist (routine routines)
  503.       (dump-fop 'lisp::fop-normal-load file)
  504.       (let ((*cold-load-dump* t))
  505.     (dump-object (car routine) file))
  506.       (dump-fop 'lisp::fop-maybe-cold-load file)
  507.       (dump-fop 'lisp::fop-assembler-routine file)
  508.       (dump-unsigned-32 (label-position (cdr routine)) file))
  509.     (let ((handle (dump-pop file)))
  510.       (dump-fixups handle fixups file)
  511.       handle)))
  512.  
  513. ;;; Dump-Fixups  --  Internal
  514. ;;;
  515. ;;;    Dump all the fixups.
  516. ;;;
  517. (defun dump-fixups (fixups file)
  518.   (declare (list fixups) (type fasl-file file))
  519.   (dolist (info fixups)
  520.     (let* ((kind (first info))
  521.        (fixup (second info))
  522.        (name (fixup-name fixup))
  523.        (flavor (fixup-flavor fixup))
  524.        (offset (third info)))
  525.       (dump-fop 'lisp::fop-normal-load file)
  526.       (let ((*cold-load-dump* t))
  527.     (dump-object kind file))
  528.       (dump-fop 'lisp::fop-maybe-cold-load file)
  529.       (ecase flavor
  530.     (:assembly-routine
  531.      (assert (symbolp name))
  532.      (dump-fop 'lisp::fop-normal-load file)
  533.      (let ((*cold-load-dump* t))
  534.        (dump-object name file))
  535.      (dump-fop 'lisp::fop-maybe-cold-load file)
  536.      (dump-fop 'lisp::fop-assembler-fixup file))
  537.     (:foreign
  538.      (assert (stringp name))
  539.      (dump-fop 'lisp::fop-foreign-fixup file)
  540.      (let ((len (length name)))
  541.        (assert (< len 256))
  542.        (dump-byte len file)
  543.        (dotimes (i len)
  544.          (dump-byte (char-code (schar name i)) file)))))
  545.       (dump-unsigned-32 offset file)))
  546.   (undefined-value))
  547.  
  548.  
  549. ;;; Dump-One-Entry  --  Internal
  550. ;;;
  551. ;;;    Dump a function-entry data structure corresponding to Entry to File.
  552. ;;; Code-Handle is the table offset of the code object for the component.
  553. ;;;
  554. ;;; If the entry is a DEFUN, then we also dump a FOP-FSET so that the cold
  555. ;;; loader can instantiate the definition at cold-load time, allowing forward
  556. ;;; references to functions in top-level forms.
  557. ;;;
  558. (defun dump-one-entry (entry code-handle file)
  559.   (declare (type entry-info entry) (type index code-handle)
  560.        (type fasl-file file))
  561.   (let ((name (entry-info-name entry)))
  562.     (dump-push code-handle file)
  563.     (dump-object name file)
  564.     (dump-object (entry-info-arguments entry) file)
  565.     (dump-object (entry-info-type entry) file)
  566.     (dump-fop 'lisp::fop-function-entry file)
  567.     (dump-unsigned-32 (label-position (entry-info-offset entry)) file)
  568.     (let ((handle (dump-pop file)))
  569.       (when (and name (symbolp name))
  570.     (dump-object name file)
  571.     (dump-push handle file)
  572.     (dump-fop 'lisp::fop-fset file))
  573.       handle)))
  574.  
  575. ;;; Alter-Code-Object  --  Internal
  576. ;;;
  577. ;;;    Alter the code object referenced by Code-Handle at the specified Offset,
  578. ;;; storing the object referenced by Entry-Handle.
  579. ;;;
  580. (defun alter-code-object (code-handle offset entry-handle file)
  581.   (declare (type index code-handle entry-handle offset) (type fasl-file file))
  582.   (dump-push code-handle file)
  583.   (dump-push entry-handle file)
  584.   (dump-fop* offset lisp::fop-byte-alter-code lisp::fop-alter-code file)
  585.   (undefined-value))
  586.  
  587.  
  588. ;;; Fasl-Dump-Component  --  Interface
  589. ;;;
  590. ;;;    Dump the code, constants, etc. for component.  We pass in the assembler
  591. ;;; fixups, code vector and node info.
  592. ;;;
  593. (defun fasl-dump-component (component code-segment length trace-table file)
  594.   (declare (type component component) (list trace-table) (type fasl-file file))
  595.  
  596.   (dump-fop 'lisp::fop-verify-empty-stack file)
  597.   (dump-fop 'lisp::fop-verify-table-size file)
  598.   (dump-unsigned-32 (fasl-file-table-free file) file)
  599.  
  600.   (let ((code-handle (dump-code-object component code-segment
  601.                        length trace-table file))
  602.     (2comp (component-info component)))
  603.     (dump-fop 'lisp::fop-verify-empty-stack file)
  604.  
  605.     (dolist (entry (ir2-component-entries 2comp))
  606.       (let ((entry-handle (dump-one-entry entry code-handle file)))
  607.     (setf (gethash entry (fasl-file-entry-table file)) entry-handle)
  608.  
  609.     (let ((old (gethash entry (fasl-file-patch-table file))))
  610.       (when old
  611.         (dolist (patch old)
  612.           (alter-code-object (car patch) (cdr patch) entry-handle file))
  613.         (remhash entry (fasl-file-patch-table file)))))))
  614.   (undefined-value))
  615.  
  616.  
  617. ;;; FASL-DUMP-TOP-LEVEL-LAMBDA-CALL  --  Interface
  618. ;;;
  619. ;;;    Dump a FOP-FUNCALL to call an already dumped top-level lambda at load
  620. ;;; time.  
  621. ;;;
  622. (defun fasl-dump-top-level-lambda-call (fun file)
  623.   (declare (type clambda fun) (type fasl-file file))
  624.   (let ((handle (gethash (leaf-info fun) (fasl-file-entry-table file))))
  625.     (assert handle)
  626.     (dump-push handle file)
  627.     (dump-fop 'lisp::fop-funcall-for-effect file)
  628.     (dump-byte 0 file))
  629.   (undefined-value))
  630.  
  631.  
  632. ;;; FASL-DUMP-SOURCE-INFO  --  Interface
  633. ;;;
  634. ;;;    Compute the correct list of DEBUG-SOURCE structures and backpatch all of
  635. ;;; the dumped DEBUG-INFO structures.  We clear the FASL-FILE-DEBUG-INFO,
  636. ;;; so that subsequent components with different source info may be dumped.
  637. ;;;
  638. (defun fasl-dump-source-info (info file)
  639.   (declare (type source-info info) (type fasl-file file))
  640.   (let ((res (debug-source-for-info info))
  641.     (*dump-only-valid-structures* nil))
  642.     (dump-object res file)
  643.     (let ((res-handle (dump-pop file)))
  644.       (dolist (info-handle (fasl-file-debug-info file))
  645.     (dump-push res-handle file)
  646.     (dump-fop 'lisp::fop-structset file)
  647.     (dump-unsigned-32 info-handle file)
  648.     (dump-unsigned-32 2 file))))
  649.  
  650.   (setf (fasl-file-debug-info file) ())
  651.   (undefined-value))
  652.  
  653.  
  654. ;;;; Main entries to object dumping:
  655.  
  656. ;;; Dump-Non-Immediate-Object  --  Internal
  657. ;;;
  658. ;;;    This function deals with dumping objects that are complex enough so that
  659. ;;; we want to cache them in the table, rather than repeatedly dumping them.
  660. ;;; If the object is in the EQ-TABLE, then we push it, otherwise, we do a type
  661. ;;; dispatch to a type specific dumping function.  The type specific branches
  662. ;;; do any appropriate EQUAL-TABLE check and table entry.
  663. ;;;
  664. ;;;    When we go to dump the object, we enter it in the CIRCULARITY-TABLE.
  665. ;;;
  666. (defun dump-non-immediate-object (x file)
  667.   (let ((index (gethash x (fasl-file-eq-table file))))
  668.     (cond ((and index (not *cold-load-dump*))
  669.        (dump-push index file))
  670.       (t
  671.        (typecase x
  672.          (symbol (dump-symbol x file))
  673.          (list
  674.           (unless (equal-check-table x file)
  675.         (dump-list x file)
  676.         (equal-save-object x file)))
  677.          (structure
  678.           (dump-structure x file)
  679.           (eq-save-object x file))
  680.          (array
  681.           (dump-array x file))
  682.          (number
  683.           (unless (equal-check-table x file)
  684.         (etypecase x
  685.           (ratio (dump-ratio x file))
  686.           (complex (dump-complex x file))
  687.           (float (dump-float x file))
  688.           (integer (dump-integer x file)))
  689.         (equal-save-object x file)))
  690.          (t
  691.           ;;
  692.           ;; This probably never happens, since bad things are detected
  693.           ;; during IR1 conversion.
  694.           (error "This object cannot be dumped into a fasl file:~% ~S"
  695.              x))))))
  696.   (undefined-value))
  697.  
  698.  
  699. ;;; Sub-Dump-Object  --  Internal
  700. ;;;
  701. ;;;    Dump an object of any type by dispatching to the correct type-specific
  702. ;;; dumping function.  We pick off immediate objects, symbols and and magic
  703. ;;; lists here.  Other objects are handled by Dump-Non-Immediate-Object.
  704. ;;;
  705. ;;; This is the function used for recursive calls to the fasl dumper.  We don't
  706. ;;; worry about creating circularities here, since it is assumed that there is
  707. ;;; a top-level call to Dump-Object.
  708. ;;;
  709. (defun sub-dump-object (x file)
  710.   (cond ((listp x)
  711.      (if x
  712.          (dump-non-immediate-object x file)
  713.          (dump-fop 'lisp::fop-empty-list file)))
  714.     ((symbolp x)
  715.      (if (eq x t)
  716.          (dump-fop 'lisp::fop-truth file)
  717.          (dump-non-immediate-object x file)))
  718.     ((fixnump x) (dump-integer x file))
  719.     ((characterp x) (dump-character x file))
  720.     (t
  721.      (dump-non-immediate-object x file))))
  722.  
  723.  
  724. ;;; Dump-Circularities  --  Internal
  725. ;;;
  726. ;;;    Dump stuff to backpatch already dumped objects.  Infos is the list of
  727. ;;; Circularity structures describing what to do.  The patching FOPs take the
  728. ;;; value to store on the stack.  We compute this value by fetching the
  729. ;;; enclosing object from the table, and then CDR'ing it if necessary.
  730. ;;;
  731. (defun dump-circularities (infos file)
  732.   (let ((table (fasl-file-eq-table file)))
  733.     (dolist (info infos)
  734.       (let* ((value (circularity-value info))
  735.          (enclosing (circularity-enclosing-object info)))
  736.     (dump-push (gethash enclosing table) file)
  737.     (unless (eq enclosing value)
  738.       (do ((current enclosing (cdr current))
  739.            (i 0 (1+ i)))
  740.           ((eq current value)
  741.            (dump-fop 'lisp::fop-nthcdr file)
  742.            (dump-unsigned-32 i file))
  743.         (declare (type index i)))))
  744.       
  745.       (ecase (circularity-type info)
  746.     (:rplaca (dump-fop 'lisp::fop-rplaca file))
  747.     (:rplacd (dump-fop 'lisp::fop-rplacd file))
  748.     (:svset (dump-fop 'lisp::fop-svset file))
  749.     (:struct-set (dump-fop 'lisp::fop-structset file)))
  750.       (dump-unsigned-32 (gethash (circularity-object info) table) file)
  751.       (dump-unsigned-32 (circularity-index info) file))))
  752.  
  753.  
  754. ;;; Dump-Object  -- Interface
  755. ;;;
  756. ;;;    Set up stuff for circularity detection, then dump an object.  All shared
  757. ;;; and circular structure will be exactly preserved within a single call to
  758. ;;; Dump-Object.  Sharing between objects dumped by separate calls is only
  759. ;;; preserved when convenient.
  760. ;;;
  761. ;;;    We peek at the objec type so that we only pay the circular detection
  762. ;;; overhead on types of objects that might be circular.
  763. ;;;
  764. (defun dump-object (x file)
  765.   (if (or (array-header-p x) (simple-vector-p x) (consp x) (structurep x))
  766.       (let ((*circularities-detected* ())
  767.         (circ (fasl-file-circularity-table file)))
  768.     (clrhash circ)
  769.     (sub-dump-object x file)
  770.     (when *circularities-detected*
  771.       (dump-circularities *circularities-detected* file)
  772.       (clrhash circ)))
  773.       (sub-dump-object x file)))
  774.  
  775.  
  776. ;;;; Load-time-value and make-load-form support.
  777.  
  778. ;;; FASL-DUMP-LOAD-TIME-VALUE-LAMBDA -- interface.
  779. ;;;
  780. ;;; Emit a funcall of the function and return the handle for the result.
  781. ;;;
  782. (defun fasl-dump-load-time-value-lambda (fun file)
  783.   (declare (type clambda fun) (type fasl-file file))
  784.   (let ((handle (gethash (leaf-info fun) (fasl-file-entry-table file))))
  785.     (assert handle)
  786.     (dump-push handle file)
  787.     (dump-fop 'lisp::fop-funcall file)
  788.     (dump-byte 0 file))
  789.   (dump-pop file))
  790.  
  791. ;;; FASL-CONSTANT-ALREADY-DUMPED -- interface.
  792. ;;;
  793. ;;; Return T iff CONSTANT has not already been dumped.  It's been dumped
  794. ;;; if it's in the EQ table.
  795. ;;; 
  796. (defun fasl-constant-already-dumped (constant file)
  797.   (if (or (gethash constant (fasl-file-eq-table file))
  798.       (gethash constant (fasl-file-valid-structures file)))
  799.       t
  800.       nil))
  801.  
  802. ;;; FASL-NOTE-HANDLE-FOR-CONSTANT -- interface.
  803. ;;;
  804. ;;; Use HANDLE whenever we try to dump CONSTANT.  HANDLE should have been
  805. ;;; returned earlier by FASL-DUMP-LOAD-TIME-VALUE-LAMBDA.
  806. ;;;
  807. (defun fasl-note-handle-for-constant (constant handle file)
  808.   (let ((table (fasl-file-eq-table file)))
  809.     (when (gethash constant table)
  810.       (error "~S already dumped?" constant))
  811.     (setf (gethash constant table) handle))
  812.   (undefined-value))
  813.  
  814. ;;; FASL-VALIDATE-STRUCTURE -- interface.
  815. ;;;
  816. ;;; Note that the specified structure can just be dumped by enumerating the
  817. ;;; slots.
  818. ;;; 
  819. (defun fasl-validate-structure (structure file)
  820.   (setf (gethash structure (fasl-file-valid-structures file)) t)
  821.   (undefined-value))
  822.  
  823.  
  824.  
  825. ;;;; Number Dumping:
  826.  
  827. ;;; Dump a ratio
  828.  
  829. (defun dump-ratio (x file)
  830.   (sub-dump-object (numerator x) file)
  831.   (sub-dump-object (denominator x) file)
  832.   (dump-fop 'lisp::fop-ratio file))
  833.  
  834. ;;; Or a complex...
  835.  
  836. (defun dump-complex (x file)
  837.   (sub-dump-object (realpart x) file)
  838.   (sub-dump-object (imagpart x) file)
  839.   (dump-fop 'lisp::fop-complex file))
  840.  
  841.  
  842. ;;; Dump an integer.
  843.  
  844. (defun dump-integer (n file)
  845.   (typecase n
  846.     ((signed-byte 8)
  847.      (dump-fop 'lisp::fop-byte-integer file)
  848.      (dump-byte (logand #xFF n) file))
  849.     ((unsigned-byte 31)
  850.      (dump-fop 'lisp::fop-word-integer file)
  851.      (dump-unsigned-32 n file))
  852.     ((signed-byte 32)
  853.      (dump-fop 'lisp::fop-word-integer file)
  854.      (dump-var-signed n 4 file))
  855.     (t
  856.      (let ((bytes (ceiling (1+ (integer-length n)) 8)))
  857.        (dump-fop* bytes lisp::fop-small-integer lisp::fop-integer file)
  858.        (dump-var-signed n bytes file)))))
  859.  
  860. (defun dump-float (x file)
  861.   (etypecase x
  862.     (single-float
  863.      (dump-fop 'lisp::fop-single-float file)
  864.      (dump-var-signed (single-float-bits x) 4 file))
  865.     (double-float
  866.      (dump-fop 'lisp::fop-double-float file)
  867.      (let ((x x))
  868.        (declare (double-float x))
  869.        (dump-unsigned-32 (double-float-low-bits x) file)
  870.        (dump-var-signed (double-float-high-bits x) 4 file)))))
  871.  
  872.  
  873. ;;;; Symbol Dumping:
  874.  
  875. ;;; Dump-Package  --  Internal
  876. ;;;
  877. ;;;    Return the table index of Pkg, adding the package to the table if
  878. ;;; necessary.  During cold load, we read the string as a normal string so that
  879. ;;; we can do the package lookup at cold load time.
  880. ;;;
  881. (defun dump-package (pkg file)
  882.   (declare (type package pkg) (type fasl-file file) (values index)
  883.        (inline assoc))
  884.   (cond ((cdr (assoc pkg (fasl-file-packages file) :test #'eq)))
  885.     (t
  886.      (unless *cold-load-dump*
  887.        (dump-fop 'lisp::fop-normal-load file))
  888.      (dump-simple-string (package-name pkg) file)
  889.      (dump-fop 'lisp::fop-package file)
  890.      (unless *cold-load-dump*
  891.        (dump-fop 'lisp::fop-maybe-cold-load file))
  892.      (let ((entry (dump-pop file)))
  893.        (push (cons pkg entry) (fasl-file-packages file))
  894.        entry))))
  895.  
  896.  
  897. ;;; Dump-Symbol  --  Internal
  898. ;;;
  899. ;;;    If we get here, it is assumed that the symbol isn't in the table, but we
  900. ;;; are responsible for putting it there when appropriate.  To avoid too much
  901. ;;; special-casing, we always push the symbol in the table, but don't record
  902. ;;; that we have done so if *Cold-Load-Dump* is true.
  903. ;;;
  904. (defun dump-symbol (s file)
  905.   (let* ((pname (symbol-name s))
  906.      (pname-length (length pname))
  907.      (pkg (symbol-package s)))
  908.  
  909.     (cond ((null pkg)
  910.        (dump-fop* pname-length lisp::fop-uninterned-small-symbol-save
  911.               lisp::fop-uninterned-symbol-save file))
  912.       ((eq pkg *package*)
  913.        (dump-fop* pname-length lisp::fop-small-symbol-save
  914.               lisp::fop-symbol-save file))
  915.       ((eq pkg ext:*lisp-package*)
  916.        (dump-fop* pname-length lisp::fop-lisp-small-symbol-save
  917.               lisp::fop-lisp-symbol-save file))
  918.       ((eq pkg ext:*keyword-package*)
  919.        (dump-fop* pname-length lisp::fop-keyword-small-symbol-save
  920.               lisp::fop-keyword-symbol-save file))
  921.       ((< pname-length 256)
  922.        (dump-fop* (dump-package pkg file)
  923.               lisp::fop-small-symbol-in-byte-package-save
  924.               lisp::fop-small-symbol-in-package-save file)
  925.        (dump-byte pname-length file))
  926.       (t
  927.        (dump-fop* (dump-package pkg file)
  928.               lisp::fop-symbol-in-byte-package-save
  929.               lisp::fop-symbol-in-package-save file)
  930.        (dump-unsigned-32 pname-length file)))
  931.  
  932.     (dump-bytes pname (length pname) file)
  933.  
  934.     (unless *cold-load-dump*
  935.       (setf (gethash s (fasl-file-eq-table file)) (fasl-file-table-free file)))
  936.  
  937.     (incf (fasl-file-table-free file)))
  938.  
  939.   (undefined-value))
  940.  
  941.  
  942. ;;; Dumper for lists.
  943.  
  944. ;;; Dump-List  --  Internal
  945. ;;;
  946. ;;;    Dump a list, setting up patching information when there are
  947. ;;; circularities.  We scan down the list, checking for CDR and CAR
  948. ;;; circularities.
  949. ;;;
  950. ;;; If there is a CDR circularity, we terminate the list with NIL and make a
  951. ;;; Circularity notation for the CDR of the previous cons.
  952. ;;;
  953. ;;; If there is no CDR circularity, then we mark the current cons and check for
  954. ;;; a CAR circularity.  When there is a CAR circularity, we make the CAR NIL
  955. ;;; initially, arranging for the current cons to be patched later.
  956. ;;;
  957. ;;; Otherwise, we recursively call the dumper to dump the current element.
  958. ;;;
  959. ;;; Marking of the conses is inhibited when *cold-load-dump* is true.  This
  960. ;;; inhibits all circularity detection.
  961. ;;;
  962. (defun dump-list (list file)
  963.   (assert (and list
  964.            (not (gethash list (fasl-file-circularity-table file)))))
  965.   (do* ((l list (cdr l))
  966.     (n 0 (1+ n))
  967.     (circ (fasl-file-circularity-table file)))
  968.        ((atom l)
  969.     (cond ((null l)
  970.            (terminate-undotted-list n file))
  971.           (t
  972.            (sub-dump-object l file)
  973.            (terminate-dotted-list n file))))
  974.     (declare (type index n))
  975.     (let ((ref (gethash l circ)))
  976.       (when ref
  977.     (push (make-circularity :type :rplacd  :object list  :index (1- n)
  978.                 :value l  :enclosing-object ref)
  979.           *circularities-detected*)
  980.     (terminate-undotted-list n file)
  981.     (return)))
  982.  
  983.     (unless *cold-load-dump*
  984.       (setf (gethash l circ) list))
  985.  
  986.     (let* ((obj (car l))
  987.        (ref (gethash obj circ)))
  988.       (cond (ref
  989.          (push (make-circularity :type :rplaca  :object list  :index n
  990.                      :value obj  :enclosing-object ref)
  991.            *circularities-detected*)
  992.          (sub-dump-object nil file))
  993.         (t
  994.          (sub-dump-object obj file))))))
  995.  
  996.  
  997. (defun terminate-dotted-list (n file)
  998.   (declare (type index n) (type fasl-file file))
  999.   (case n
  1000.     (1 (dump-fop 'lisp::fop-list*-1 file))
  1001.     (2 (dump-fop 'lisp::fop-list*-2 file))
  1002.     (3 (dump-fop 'lisp::fop-list*-3 file))
  1003.     (4 (dump-fop 'lisp::fop-list*-4 file))
  1004.     (5 (dump-fop 'lisp::fop-list*-5 file))
  1005.     (6 (dump-fop 'lisp::fop-list*-6 file))
  1006.     (7 (dump-fop 'lisp::fop-list*-7 file))
  1007.     (8 (dump-fop 'lisp::fop-list*-8 file))
  1008.     (T (do ((nn n (- nn 255)))
  1009.        ((< nn 256)
  1010.         (dump-fop 'lisp::fop-list* file)
  1011.         (dump-byte nn file))
  1012.      (declare (type index nn))
  1013.      (dump-fop 'lisp::fop-list* file)
  1014.      (dump-byte 255 file)))))
  1015.  
  1016. ;;; If N > 255, must build list with one list operator, then list* operators.
  1017.  
  1018. (defun terminate-undotted-list (n file)
  1019.   (declare (type index n) (type fasl-file file))
  1020.   (case n
  1021.     (1 (dump-fop 'lisp::fop-list-1 file))
  1022.     (2 (dump-fop 'lisp::fop-list-2 file))
  1023.     (3 (dump-fop 'lisp::fop-list-3 file))
  1024.     (4 (dump-fop 'lisp::fop-list-4 file))
  1025.     (5 (dump-fop 'lisp::fop-list-5 file))
  1026.     (6 (dump-fop 'lisp::fop-list-6 file))
  1027.     (7 (dump-fop 'lisp::fop-list-7 file))
  1028.     (8 (dump-fop 'lisp::fop-list-8 file))
  1029.     (T (cond ((< n 256)
  1030.           (dump-fop 'lisp::fop-list file)
  1031.           (dump-byte n file))
  1032.          (t (dump-fop 'lisp::fop-list file)
  1033.         (dump-byte 255 file)
  1034.         (do ((nn (- n 255) (- nn 255)))
  1035.             ((< nn 256)
  1036.              (dump-fop 'lisp::fop-list* file)
  1037.              (dump-byte nn file))
  1038.           (declare (type index nn))
  1039.           (dump-fop 'lisp::fop-list* file)
  1040.           (dump-byte 255 file)))))))
  1041.  
  1042.  
  1043. ;;;; Array dumping:
  1044.  
  1045. ;;; DUMP-ARRAY  --  Internal.
  1046. ;;;
  1047. ;;; Dump the array thing.
  1048. ;;;
  1049. (defun dump-array (x file)
  1050.   (if (vectorp x)
  1051.       (dump-vector x file)
  1052.       (dump-multi-dim-array x file)))
  1053.  
  1054. ;;; DUMP-VECTOR  --  Internal.
  1055. ;;;
  1056. ;;; Dump the vector object.  If it's not simple, then actually dump a simple
  1057. ;;; version of it.  But we enter the original in the EQ or EQUAL tables.
  1058. ;;; 
  1059. (defun dump-vector (x file)
  1060.   (let ((simple-version (if (array-header-p x)
  1061.                 (coerce x 'simple-array)
  1062.                 x)))
  1063.     (typecase simple-version
  1064.       (simple-base-string
  1065.        (unless (equal-check-table x file)
  1066.      (dump-simple-string simple-version file)
  1067.      (equal-save-object x file)))
  1068.       (simple-vector
  1069.        (dump-simple-vector simple-version file)
  1070.        (eq-save-object x file))
  1071.       ((simple-array single-float (*))
  1072.        (dump-single-float-vector simple-version file)
  1073.        (eq-save-object x file))
  1074.       ((simple-array double-float (*))
  1075.        (dump-double-float-vector simple-version file)
  1076.        (eq-save-object x file))
  1077.       (t
  1078.        (dump-i-vector simple-version file)
  1079.        (eq-save-object x file)))))
  1080.  
  1081. ;;; DUMP-SIMPLE-VECTOR  --  Internal
  1082. ;;;
  1083. ;;;    Dump a SIMPLE-VECTOR, handling any circularities.
  1084. ;;;
  1085. (defun dump-simple-vector (v file)
  1086.   (declare (type simple-vector v) (type fasl-file file))
  1087.   (note-potential-circularity v file)
  1088.   (do ((index 0 (1+ index))
  1089.        (length (length v))
  1090.        (circ (fasl-file-circularity-table file)))
  1091.       ((= index length)
  1092.        (dump-fop* length lisp::fop-small-vector lisp::fop-vector file))
  1093.     (let* ((obj (aref v index))
  1094.        (ref (gethash obj circ)))
  1095.       (cond (ref
  1096.          (push (make-circularity :type :svset  :object v  :index index
  1097.                      :value obj  :enclosing-object ref)
  1098.            *circularities-detected*)
  1099.          (sub-dump-object nil file))
  1100.         (t
  1101.          (sub-dump-object obj file))))))
  1102.  
  1103. ;;; DUMP-SIMPLE-STRING  --  Internal
  1104. ;;;
  1105. ;;;    Dump a SIMPLE-BASE-STRING.
  1106. ;;;
  1107. (defun dump-simple-string (s file)
  1108.   (declare (type simple-base-string s))
  1109.   (let ((length (length s)))
  1110.     (dump-fop* length lisp::fop-small-string lisp::fop-string file)
  1111.     (dump-bytes s length file))
  1112.   (undefined-value))
  1113.  
  1114. ;;; DUMP-I-VECTOR  --  Internal
  1115. ;;;
  1116. ;;; *** NOT *** the FOP-INT-VECTOR as currently documented in rtguts.  Size
  1117. ;;; must be a directly supported I-vector element size, with no extra bits.
  1118. ;;;
  1119. ;;; If a byte vector, or if the native and target byte orderings are the same,
  1120. ;;; then just write the bits.  Otherwise, dispatch off of the target byte order
  1121. ;;; and write the vector one element at a time.
  1122. ;;;
  1123. (defun dump-i-vector (vec file &optional data-only)
  1124.   (declare (type (simple-array * (*)) vec))
  1125.   (let* ((ac (etypecase vec
  1126.            (simple-bit-vector 0)
  1127.            ((simple-array (unsigned-byte 2) (*)) 1)
  1128.            ((simple-array (unsigned-byte 4) (*)) 2)
  1129.            ((simple-array (unsigned-byte 8) (*)) 3)
  1130.            ((simple-array (unsigned-byte 16) (*)) 4)
  1131.            ((simple-array (unsigned-byte 32) (*)) 5)))
  1132.      (len (length vec))
  1133.      (size (ash 1 ac))
  1134.      (bytes (ash (+ (the index (ash len ac)) 7) -3)))
  1135.     (declare (type index ac len size bytes))
  1136.     (unless data-only
  1137.       (dump-fop 'lisp::fop-int-vector file)
  1138.       (dump-unsigned-32 len file)
  1139.       (dump-byte size file))
  1140.     (dump-data-maybe-byte-swapping vec bytes size file)))
  1141.  
  1142. ;;; DUMP-SINGLE-FLOAT-VECTOR  --  internal.
  1143. ;;; 
  1144. (defun dump-single-float-vector (vec file)
  1145.   (let ((length (length vec)))
  1146.     (dump-fop 'lisp::fop-single-float-vector file)
  1147.     (dump-unsigned-32 length file)
  1148.     (dump-data-maybe-byte-swapping vec (* length vm:word-bytes)
  1149.                    vm:word-bytes file)))
  1150.  
  1151. ;;; DUMP-DOUBLE-FLOAT-VECTOR  --  internal.
  1152. ;;; 
  1153. (defun dump-double-float-vector (vec file)
  1154.   (let ((length (length vec)))
  1155.     (dump-fop 'lisp::fop-double-float-vector file)
  1156.     (dump-unsigned-32 length file)
  1157.     (dump-data-maybe-byte-swapping vec (* length vm:word-bytes 2)
  1158.                    (* vm:word-bytes 2) file)))
  1159.  
  1160. ;;; DUMP-DATA-BITS-MAYBE-BYTE-SWAPPING  --  internal.
  1161. ;;;
  1162. ;;; Dump BYTES of data from DATA-VECTOR (which must be some unboxed vector)
  1163. ;;; byte-swapping if necessary.
  1164. ;;; 
  1165. (defun dump-data-maybe-byte-swapping (data-vector bytes element-size file)
  1166.   (declare (type (simple-array * (*)) data-vector)
  1167.        (type unsigned-byte bytes)
  1168.        (type (integer 1) element-size))
  1169.   (cond ((or (eq (backend-byte-order *backend*)
  1170.          (backend-byte-order *native-backend*))
  1171.          (= element-size vm:byte-bits))
  1172.      (dump-bytes data-vector bytes file))
  1173.     ((>= element-size vm:word-bits)
  1174.      (let ((words-per-element (/ element-size vm:word-bits))
  1175.            (result (make-array bytes :element-type '(unsigned-byte 8))))
  1176.        (declare (type (integer 1 #.most-positive-fixnum)
  1177.               words-per-element))
  1178.        (dotimes (index (the integer (/ bytes words-per-element)))
  1179.          (dotimes (offset words-per-element)
  1180.            (let ((word (%raw-bits data-vector
  1181.                       (+ (* index words-per-element)
  1182.                      vm:vector-data-offset
  1183.                      (1- words-per-element)
  1184.                      (- offset)))))
  1185.          (setf (%raw-bits result (+ (* index words-per-element)
  1186.                         vm:vector-data-offset
  1187.                         offset))
  1188.                (logior (ash (ldb (byte 8 0) word) 24)
  1189.                    (ash (ldb (byte 8 8) word) 16)
  1190.                    (ash (ldb (byte 8 16) word) 8)
  1191.                    (ldb (byte 8 24) word))))))
  1192.        (dump-bytes result bytes file)))
  1193.     ((> element-size vm:byte-bits)
  1194.      (let* ((bytes-per-element (/ element-size vm:byte-bits))
  1195.         (elements (/ bytes bytes-per-element))
  1196.         (result (make-array elements
  1197.                     :element-type
  1198.                     `(unsigned-byte ,element-size))))
  1199.        (declare (type (integer 1 #.most-positive-fixnum)
  1200.               bytes-per-element)
  1201.             (type unsigned-byte elements))
  1202.        (dotimes (index elements)
  1203.          (let ((element (aref data-vector index))
  1204.            (new-element 0))
  1205.            (dotimes (i bytes-per-element)
  1206.          (setf new-element
  1207.                (logior (ash new-element vm:byte-bits)
  1208.                    (ldb (byte vm:byte-bits 0) element)))
  1209.          (setf element (ash element (- vm:byte-bits))))
  1210.            (setf (aref result index) new-element)))
  1211.        (dump-bytes result bytes file)))
  1212.     (t
  1213.      (let* ((elements-per-byte (/ vm:byte-bits element-size))
  1214.         (elements (* bytes elements-per-byte))
  1215.         (len (length data-vector))
  1216.         (result (make-array elements
  1217.                     :element-type
  1218.                     `(unsigned-byte ,element-size))))
  1219.        (dotimes (index elements)
  1220.          (multiple-value-bind (byte-index additional)
  1221.                   (truncate index elements-per-byte)
  1222.            (let ((src-idx (+ byte-index
  1223.                  (- elements-per-byte additional 1))))
  1224.          (setf (aref result index)
  1225.                (if (>= src-idx len)
  1226.                0
  1227.                (aref data-vector src-idx))))))
  1228.        (dump-bytes result bytes file)))))
  1229.  
  1230. ;;; Dump-Multi-Dim-Array  --  Internal
  1231. ;;;
  1232. ;;; Dump a multi-dimensional array.  Note: any displacements are folded out.
  1233. ;;;
  1234. (defun dump-multi-dim-array (array file)
  1235.   (let ((rank (array-rank array)))
  1236.     (dotimes (i rank)
  1237.       (dump-integer (array-dimension array i) file))
  1238.     (lisp::with-array-data ((vector array) (start) (end))
  1239.       (if (and (= start 0) (= end (length vector)))
  1240.       (sub-dump-object vector file)
  1241.       (sub-dump-object (subseq vector start end) file)))
  1242.     (dump-fop 'lisp::fop-array file)
  1243.     (dump-unsigned-32 rank file)
  1244.     (eq-save-object array file)))
  1245.  
  1246.  
  1247. ;;; Dump a character.
  1248.  
  1249. (defun dump-character (ch file)
  1250.   (dump-fop 'lisp::fop-short-character file)
  1251.   (dump-byte (char-code ch) file))
  1252.  
  1253.  
  1254. ;;; Dump a structure.
  1255.  
  1256. (defun dump-structure (struct file)
  1257.   (when *dump-only-valid-structures*
  1258.     (unless (gethash struct (fasl-file-valid-structures file))
  1259.       (error "Attempt to dump invalid structure:~%  ~S~%How did this happen?"
  1260.          struct)))
  1261.   (note-potential-circularity struct file)
  1262.   (do ((index 0 (1+ index))
  1263.        (length (structure-length struct))
  1264.        (circ (fasl-file-circularity-table file)))
  1265.       ((= index length)
  1266.        (dump-fop* length lisp::fop-small-struct lisp::fop-struct file))
  1267.     (let* ((obj (structure-ref struct index))
  1268.        (ref (gethash obj circ)))
  1269.       (cond (ref
  1270.          (push (make-circularity :type :struct-set
  1271.                      :object struct
  1272.                      :index index
  1273.                      :value obj
  1274.                      :enclosing-object ref)
  1275.            *circularities-detected*)
  1276.          (sub-dump-object nil file))
  1277.         (t
  1278.          (sub-dump-object obj file))))))
  1279.  
  1280.